package com.google.code.irouter.portal.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.code.irouter.model.Consumer;
import com.google.code.irouter.model.History;
import com.google.code.irouter.model.Model;
import com.google.code.irouter.model.Queue;
import com.google.code.irouter.model.Relationship;
import com.google.code.irouter.model.Solution;
import com.google.code.irouter.portal.client.rpc.queue.QueueResult;
import com.google.code.irouter.portal.client.rpc.queue.QueueService;
import com.google.code.irouter.utils.DateTimeUtil;
import com.google.code.irouter.utils.ModelUtil;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class QueueServiceImpl extends RemoteServiceServlet implements QueueService {

	private static final long serialVersionUID = 2152604267178084678L;

	@Override
	public List<QueueResult> load(String solutionId)
			throws IllegalArgumentException {
		List<QueueResult> queueResultList = null;
		try {
			List<Model> objectList = new ArrayList<Model>();
			Map<String, Object> cond = new HashMap<String, Object>();
			Map<String, SortDirection> order = new HashMap<String, SortDirection>();
			order.put("createDate", SortDirection.DESCENDING);
			Key solutionKey = KeyFactory.createKey(Solution.class.getSimpleName(), Long.valueOf(solutionId));
			cond.put("solution", solutionKey);
			objectList = ModelUtil.selectByOrder(Queue.class, cond, order);			
			queueResultList = new ArrayList<QueueResult>();
			
			List<Model> solutionList = ModelUtil.select(Solution.class, solutionKey);
			String solutionName = "";
			if(solutionList.size() > 0)
				solutionName = ((Solution)solutionList.get(0)).getName();	
			for(Object object : objectList){
				Queue queue = (Queue) object;
				QueueResult queueResult = new QueueResult();
				queueResult.setId(queue.getKey().getId());
				List<Model> consumerList = ModelUtil.select(Consumer.class, queue.getConsumer());
				if(consumerList.size() > 0)
					queueResult.setConsumer(((Consumer)consumerList.get(0)).getName());
				else
					queueResult.setConsumer("");
				List<Model> relationshipList = ModelUtil.select(Relationship.class, queue.getRelationship());
				if(relationshipList.size() > 0)
					queueResult.setRelationship(((Relationship)relationshipList.get(0)).getName());
				else
					queueResult.setRelationship("");
				queueResult.setSolution(solutionName);
				queueResult.setMessage(queue.getMessage());
				queueResult.setLink(queue.getLink().getValue());
				queueResult.setFixedLink(queue.getFixedLink().getValue());
				queueResult.setCreateDate(DateTimeUtil.formatDate(queue.getCreateDate()));
				queueResult.setUpdateDate(DateTimeUtil.formatDate(queue.getUpdateDate()));
				queueResultList.add(queueResult);
			}
		} catch (Exception exception) {
			// TODO
		}
		return queueResultList;
	}

	@Override
	public boolean remove(String[] ids) throws IllegalArgumentException {
		boolean result = true;
		try {
			List<Key> idList = new ArrayList<Key>();
			for(String id : ids) {
				Key key = KeyFactory.createKey(Queue.class.getSimpleName(), Long.valueOf(id));
				List<Model> modelList = ModelUtil.select(Queue.class, key);
				if(modelList.size() > 0) {
					idList.add(key);
					List<Model> historyList = ModelUtil.select(History.class, ((Queue) modelList.get(0)).getHistory());
					if(historyList.size() > 0)
						ModelUtil.remove((History) historyList.get(0));
				}				
			}
			ModelUtil.remove(idList);
		} catch (Exception exception) {
			// TODO
			result = false;
		}
		return result;
	}
}
